package com.shiftl.data.aroma.dao;

import java.util.Collection;
import java.util.LinkedList;

import javax.jdo.PersistenceManager;

import com.google.appengine.api.datastore.Key;
import com.shiftl.data.aroma.entity.IncenseEnt;
import com.shiftl.data.aroma.model.Incense;
import com.shiftl.data.jdo.PMF;

public class IncenseDAO {

	public IncenseDAO() {

	}

	public Incense getIncense(final Object id) {
		final PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			return pm.getObjectById(IncenseEnt.class, id);
		} finally {
			pm.close();
		}

	}

	public Collection<Incense> getIngredientMulti(final Collection<Key> keys) {
		final PersistenceManager pm = PMF.get().getPersistenceManager();
		// Collection<String> ids = ORMUtil.keysToStrings(keys);
		final Collection<Incense> incenses = new LinkedList<Incense>();
		try {
			// for(String id: ids){
			// ingrds.add(pm.getObjectById(IngredientEnt.class,id));
			// }

			for (final Key key : keys) {
				incenses.add(pm.getObjectById(IncenseEnt.class, key));
			}
			return incenses;
		} finally {
			pm.close();
		}

	}

	public Collection<Incense> getLatestEntries(final int numEntries) {
		final PersistenceManager pm = PMF.get().getPersistenceManager();
		// Collection<String> ids = ORMUtil.keysToStrings(keys);
		final Collection<Incense> incenses = new LinkedList<Incense>();
		try {
			// for(String id: ids){
			// ingrds.add(pm.getObjectById(IngredientEnt.class,id));
			// }
			Collection<Key> keys = new LinkedList<Key>();

			for (final Key key : keys) {
				incenses.add(pm.getObjectById(IncenseEnt.class, key));
			}
			return incenses;
		} finally {
			pm.close();
		}
	}

	public void saveIncense(final Incense incense) {
		final PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			pm.makePersistent((IncenseEnt) incense);
		} finally {
			pm.close();
		}
	}

}
